package com.fintech.third.service.interfacedata;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fintech.common.utils.PageConverter;
import com.fintech.third.dao.InterfaceCallCountLogMapper;
import com.fintech.third.dao.interfacedata.EdcInterfaceCodeInfoMapper;
import com.fintech.third.entity.log.InterfaceCallCountInfo;
import com.fintech.third.model.interfacedata.EdcInterfaceCodeInfoDto;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.utils.MarvelsLogUtil;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import net.sf.json.JSONObject;

@Service("interfaceCodeInfoService")
public class InterfaceCodeInfoImpl implements InterfaceCodeInfoService{
	@Autowired
	private EdcInterfaceCodeInfoMapper edcInterfaceCodeInfoMapper;
	@Autowired
	private InterfaceCallCountLogMapper interfaceCallCountLogMapper;
	
	@Override
	public List<EdcInterfaceCodeInfoDto> edcInterfaceCodeInfo() {
		return edcInterfaceCodeInfoMapper.selectEdcInterfaceCodeInfo();
	}
	

	/**
	 * 获取账单费用类型
	 * @return
	 */
	@Override
	public List<InterfaceCallCountInfo> selectInterfaceConfigType() {
		List<InterfaceCallCountInfo> list = interfaceCallCountLogMapper.selectInterfaceConfigType();
		return list;
	}
	

	/**
	 * 获取账单费用类型
	 * @return
	 */
//	@Override
	/*public Map<String, Object> selectInterfacePaymentBill(Map param) {
		MarvelsLogUtil.getInstance().info("PaymentBillImpl selectInterfacePaymentBill start ,param = " + JSONObject.fromObject(param));
		String startDate = (String) param.get("startDate");// 统计起始时间
		String endDate = (String) param.get("endDate");// 统计结束时间
		
		if(StringUtils.isNotBlank(endDate)){
			endDate = endDate.substring(0, 10);
		}else{
			endDate = DateUtil.dataFormat3(new Date());
		}
		
		if(StringUtils.isNotBlank(startDate)){
			startDate = startDate.substring(0, 10);
		}else {
			//日期为空时 显示 以结尾日期的前一天计算
			startDate = DateUtil.add2(endDate,"yyyy-MM-dd",5,-1);
		}
		
		String createdBy = (String) param.get("createdBy");// 统计人姓名
		String createdByMobile = (String) param.get("createdByMobile");// 手机号
		String billType = (String) param.get("billType");// 账单类型// 账单类型
		Integer pageNo = (Integer) param.get("pageNo");// 账单类型
		Integer pageSize = (Integer) param.get("pageSize");// 账单类型
		if (pageNo == null || pageNo <= 0){
			pageNo = 1;
		}
		if (pageSize == null || pageSize <= 0){
			pageSize = 2;
		}
		
			
		try {
			String[] userArray = null;
			
			if (StringUtils.isNotBlank(createdBy)) {
				userArray = createdBy.split(",");
			}
			
			
			List<Map<String,Object>> resultList = new ArrayList();
			
			if (StringUtils.isBlank(billType) || "0".equals(billType)) {
				PageHelper.startPage(pageNo, pageSize);
				resultList = interfaceCallCountLogMapper.selectInterfacePaymentBill(userArray, startDate, endDate, createdByMobile);
			}else if("1".equals(billType)){
				PageHelper.startPage(pageNo, pageSize);
				resultList = interfaceCallCountLogMapper.selectInterfacePaymentBill1(userArray, startDate, endDate, createdByMobile);
			}else {
				PageHelper.startPage(pageNo, pageSize);
				resultList = interfaceCallCountLogMapper.selectInterfacePaymentBill2(userArray, startDate, endDate, createdByMobile,billType);
			}
			
			if (CollectionUtils.isEmpty(resultList)) {
				return PageConverter.converter(new PageInfo<Map<String,Object>>(new ArrayList<Map<String,Object>>()));
			}
			
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(resultList);
			return PageConverter.converter(pageInfo);
			
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return PageConverter.converter(new PageInfo<Map<String,Object>>(new ArrayList<Map<String,Object>>()));
		}

	}*/
	/**
	 * 新的支付账单 需求由董总提出
	 * @author 刘琴
	 * @date 2018-11-02
	 * @param param
	 * @return
	 */
	@Override
	public Map<String, Object> selectInterfacePaymentBill(Map param) {
		MarvelsLogUtil.getInstance().info("PaymentBillImpl selectInterfacePaymentBill start ,param = " + JSONObject.fromObject(param));
		String startDate = (String) param.get("startDate");// 统计起始时间
		String endDate = (String) param.get("endDate");// 统计结束时间
		
		if(StringUtils.isNotBlank(endDate)){
			endDate = endDate.substring(0, 10);
		}
		
		if(StringUtils.isNotBlank(startDate)){
			startDate = startDate.substring(0, 10);
		}
		
		String createdBy = (String) param.get("createdBy");// 统计人姓名
		String createdByMobile = (String) param.get("createdByMobile");// 手机号
		String billType = (String) param.get("billType");// 账单类型// 账单类型
		Integer pageNo = (Integer) param.get("pageNo");// 账单类型
		Integer pageSize = (Integer) param.get("pageSize");// 账单类型
		if (pageNo == null || pageNo <= 0){
			pageNo = 1;
		}
		if (pageSize == null || pageSize <= 0){
			pageSize = 10;
		}
		
			
		try {
			String[] userArray = null;
			
			if (StringUtils.isNotBlank(createdBy)) {
				userArray = createdBy.split(",");
			}

			List<Map<String,Object>> resultList = new ArrayList();
			
			PageHelper.startPage(pageNo, pageSize);
			resultList = interfaceCallCountLogMapper.selectInterfacePaymentBillTemp1(userArray, startDate, endDate, createdByMobile,billType);
			Double totleAmount = 0.00d;
			if (CollectionUtils.isEmpty(resultList)) {
				return PageConverter.converter(new PageInfo<Map<String,Object>>(new ArrayList<Map<String,Object>>()));
			}else{
				List<String> billTypes = new ArrayList<String>();
				if(StrUtil.isNotEmpty(billType)){
					billTypes.add(billType);
				}
				totleAmount = interfaceCallCountLogMapper.countInterfacePaymentBillTemp1(userArray, startDate, endDate, createdByMobile,billTypes);
			}
			
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(resultList);
			Map<String, Object> resultmap = PageConverter.converter(pageInfo);
			resultmap.put("totleAmount", totleAmount);
			return resultmap;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return PageConverter.converter(new PageInfo<Map<String,Object>>(new ArrayList<Map<String,Object>>()));
		}

	}
	/**
	 * 新的支付账单 需求由董总提出
	 * @author 刘琴
	 * @date 2018-11-02
	 * @param param
	 * @return
	 */
	@Override
	public Map<String, Object> selectInterfacePaymentBill2(Map param) {
		MarvelsLogUtil.getInstance().info("PaymentBillImpl selectInterfacePaymentBill start ,param = " + JSONObject.fromObject(param));
		String startDate = (String) param.get("startDate");// 统计起始时间
		String endDate = (String) param.get("endDate");// 统计结束时间
		List<String> dataTypeList = (List<String>) param.get("dataType");
		//List<String> dataTypeList = JSONUtil.parseArray(dataTypes).toList(String.class);
		
		if(StringUtils.isNotBlank(endDate)){
			endDate = endDate.substring(0, 10);
		}
		
		if(StringUtils.isNotBlank(startDate)){
			startDate = startDate.substring(0, 10);
		}
		
		Integer pageNo = (Integer) param.get("pageNo");// 账单类型
		Integer pageSize = (Integer) param.get("pageSize");// 账单类型
		if (pageNo == null || pageNo <= 0){
			pageNo = 1;
		}
		if (pageSize == null || pageSize <= 0){
			pageSize = 10;
		}
		try {

			List<Map<String,Object>> resultList = new ArrayList();
			
			PageHelper.startPage(pageNo, pageSize);
			resultList = interfaceCallCountLogMapper.selectInterfacePaymentBillTemp2(startDate, endDate);
			Double totleAmount = 0.00d;
			if (CollectionUtils.isEmpty(resultList)) {
				return PageConverter.converter(new PageInfo<Map<String,Object>>(new ArrayList<Map<String,Object>>()));
			}else{
				totleAmount = interfaceCallCountLogMapper.countInterfacePaymentBillTemp1(null, startDate, endDate, null,dataTypeList);
			}
			
			PageInfo<Map<String,Object>> pageInfo = new PageInfo<Map<String,Object>>(resultList);
			Map<String, Object> resultmap = PageConverter.converter(pageInfo);
			resultmap.put("totleAmount", totleAmount);
			return resultmap;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return PageConverter.converter(new PageInfo<Map<String,Object>>(new ArrayList<Map<String,Object>>()));
		}

	}
	
}
